package com.jml.disruptortest;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

//定义生产这发送事件
public class LongEventProducer {

    public final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(ByteBuffer byteBuffer) {
        // 1.拿到环形的序列位置，事件的下标位置
        long sequence = ringBuffer.next();
        Long data = null;
        try {
            //2.取出空的事件队列
            LongEvent longEvent = ringBuffer.get(sequence);
            //3.获取事件队列传递的数据，组装发给消费者   把第0个值传过去
            longEvent.setValue(byteBuffer.getLong(0));
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } finally {
            System.out.println("生产这准备发送数据");
            //4.发布事件
            //放在finally是为了防止已经组装好数据后，发生异常
            ringBuffer.publish(sequence);

        }
    }

}

